# frozen_string_literal: tr
class V1::InterviewsController < V1::ApplicationController

  before_action :authenticate_editor_by_token

  def index
    @interviews =
      if @current_editor.estate?
        @current_editor.village.interviews.order(created_at: :desc).page(page).per(per)
      elsif @current_editor.floor?
        building_ids = @current_editor.buildings.pluck(:id)

        @interviews = @current_editor.village.interviews.where(building_id: building_ids).order(created_at: :desc).page(page).per(per)
      end

    paginate @interviews
  end

  def create
    village = @current_editor.village
    uploaded_io = params[:photo]
    photo_name = uploaded_io.original_filename if uploaded_io
    idcard = Idcard.where(
      name: params[:name],
      gender: params[:gender],
      nation: params[:nation],
      photo: photo_name,
      birthday: params[:birthday],
      code: params[:card_code],
      card_valid: params[:card_valid],
      started_at: params[:started_at],
      expired_at: params[:expired_at]
    ).first
      logger.info "idcard: #{idcard.id if idcard}"
    if idcard.nil?
        logger.info "创建记录"
      idcard = Idcard.create(
        name: params[:name],
        gender: params[:gender],
        nation: params[:nation],
        photo: params[:photo],
        birthday: params[:birthday],
        code: params[:card_code],
        card_valid: params[:card_valid],
        started_at: params[:started_at],
        expired_at: params[:expired_at]
      )
    end

    guest = Guest.find_or_create_by(
      idcard: idcard,
      name: params[:name]
    )

    # 需要实现
    keyword = params[:keyword].strip if params[:keyword]

    user = User.where(telephone: keyword).first

    raise CommonError::CustomMessageError.new('没有这个业主') if user.blank?

    building = user.building

    interview = Interview.new(
      door_name: params[:door_name],
      reason: params[:reason],
      flag: params[:flag],
      access_code: params[:access_code],
      keyword: params[:keyword],
      entered_at: params[:entered_at],
      guest: guest,
      village: village,
      building: building,
      user: user,
      card_code: params[:card_code]
    )

    interview.save!
    
    content = "#{guest.name} 访问 #{user.name}"


    extras = {
      content: content,
      photo: idcard.photo.url,
      name: params[:name],
      gender: params[:gender],
      nation: params[:nation],
      birthday: params[:birthday],
      code: params[:card_code],
      interview_id: interview.id,
      card_valid: params[:card_valid],
      started_at: params[:started_at],
      expired_at: params[:expired_at]
    }

    content = "#{params[:name]} 想进来"
    extras2 = {
      content: content,
      guest_id: guest.id.to_s,
      editor_id: @current_editor.id.to_s,
      name: params[:name],
      gender: params[:gender],
      nation: params[:nation],
      birthday: params[:birthday],
      photo: idcard.photo.url,
      code: params[:card_code],
      interview_id: interview.id,
      card_valid: params[:card_valid],
      started_at: params[:started_at],
      expired_at: params[:expired_at],
      floor_code: user.floor_code, 
      building_code: user.building.code,
      village_name: user.village.name,
      user_telephone: user.telephone
    }

    message = []
    id = interview.id
    if user.registration_id.blank?
      message << "没有'#{user.name}'业主极光推送ID"
    else
      JpushServiceJob.perform_later(id,user.registration_id,extras2,content)
      logger.info "user: #{user.registration_id}"
    end

    #楼主极光推送
    editor_floor = building.editor
    editor_registration_id = editor_floor.registration_id
    if editor_floor.nil? or editor_registration_id.blank?
      message << "没有楼主管理员极光推送ID"
    else
      JpushServiceJob.perform_later(id,editor_registration_id,extras,content)
      logger.info "editor: #{editor_registration_id}"
    end

    #门卫极光推送
    editor_door = village.editors.with_role(:door).first
    if editor_door.nil? or editor_door.registration_id.blank?
      message << "没有门卫极光推送ID"
    else
      JpushServiceJob.perform_later(id,editor_door.registration_id,extras2,content)
      logger.info "editor_door: #{editor_door.registration_id}"
    end

    #片区民警极光推送
    editor_estate = village.editors.with_role(:estate).first
    if editor_estate.nil? or editor_estate.registration_id.blank?
      message << "没有片区民警极光推送ID"
    else
      JpushServiceJob.perform_later(id,editor_estate.registration_id, extras2, content)
      logger.info "editor_estate: #{editor_estate.registration_id}"
    end

    interview_id = interview.id

    #head 201guest.id, editor_id: editior
    render json: { message: message.join(','), interview_id: interview_id,
                   guest_id: guest.id, editor_id: @current_editor.id, 
                   floor_code: user.floor_code,  building_code: user.building.code,
                   village_name: user.village.name, user_telephone: user.telephone}
  end

  def exit
    keyword = params[:keyword]
    puts "xxx"
    puts  params[:access_code]  
    #interview = Interview.where(card_code: card_code).order(created_at: :desc).first
    interview = Interview.where('access_code = :value OR card_code = :value',{value: params[:access_code]})
                        .order(created_at: :desc).first
    if interview
      interview.update(exited_at: params[:exited_at])
    end

    render json: { keyword: params[:keyword], exited_at: params[:exited_at]}

    #head 201
  end
end

# {
#   "name": "来访人名字",
#   "gender": "来访人性别",
#   "nation": "来访人民族",
#   "photo": "来访人身份证头像 base64",
#   "birthday": "来访人生日",
#   "address": "来访人身份证住址",
#   "card_code": "身份证号码",
#   "card_valid": true,
#   "expired_at": "身份证过期时间",
#   "entered_at": "进入时间",
#   "car_code": "3456"
#   "exited_at": "离开时间",
#   "reason": "来访原因",
#   "flag": true,
#   "access_code": "小票号码",
#   "keyword": "受访者的 门牌 地址 手机号码",
#   "door_name": "小区门"
# }
